<template>
  <div class="tocaselotto">
    <!-- <div v-for="(item, i) in scrollList" :key="i" v-if="i == index">
      <div class="scroll" v-if="scrollShow">
        <van-image round width="16px" height="16px" :src="item.avatar" />
        <span class="name">{{ item.scroll_name }}</span>
      </div>
    </div> -->

    <div class="top">
      <div class="up">
        <div class="img"><img src="../assets/img/cj.png" alt="" /></div>
        <div></div>
        <div class="title">{{ activityItem.merchantname }}</div>
        <div class="text" style="color: #f6f690">
          <span>剩余{{ draw || 0.0 }}次</span>
          <span class="shu">|</span>
          <div class="times">
            <span>{{ dd }}</span>天<span>{{ hh }}</span
            >小时<span>{{ mm }}</span
            >分<span>{{ ss }}</span
            >秒
          </div>
        </div>
      </div>
      <LuckyWheel
        :style="{
          width: '23.5rem',
          height: '23.5rem',
          margin: '0 auto',
          marginTop: '1rem',
        }"
        ref="LuckyWheel"
        :default-style="{
          fontColor: '#d64737',
          fontSize: '12px',
        }"
        :blocks="[{ padding: '13px', background: '#d64737' }]"
        :prizes="prizesnew"
        :buttons="[
          { radius: '50px', background: '#d64737' },
          { radius: '45px', background: '#fff' },
          { radius: '41px', background: '#f6c66f', pointer: true },
          {
            radius: '35px',
            background: '#ffdea0',
            imgs: [
              {
                src: require('../assets/img/anniu.png'),
                width: '80%',
                top: '-45%',
              },
            ],
          },
        ]"
        @start="startCallBack"
        @end="endCallBack"
      />
    </div>

    <!-- 中奖弹窗 -->
    <div class="mask" v-if="showAdress"></div>
    <div class="lottery-alert" v-if="showAdress">
      <span class="close-icon" @click="btnClick"
        ><van-icon color="white" name="cross"
      /></span>
      <h1 v-if="prizesold[indexs].name == '谢谢参与'">很遗憾</h1>
      <h1 v-else>恭喜您</h1>
      <p><img :src="prizesold[indexs].picture" alt="" /></p>
      <div v-if="prizesold[indexs].name == '谢谢参与'"></div>
      <template v-else>
        <h2>获得{{ prizesold[indexs].name }}</h2>
        <div class="btnsave" @click="adressClick" v-if="prize_type == 2">
          请填写收货地址,以便商家发送奖品
        </div>
        <div class="btnsave" @click="sinceClick" v-if="prize_type == 1">
          请联系商家,核销奖品
        </div>
      </template>

      <div
        class="btnsave"
        @click="continueClick"
        v-if="prizesold[indexs].name == '谢谢参与'"
      >
        继续抽奖
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from "vant";
import LuckDraw from "vue-luck-draw";
Vue.use(LuckDraw);
export default {
  components: {
    [Icon.name]: Icon,
  },
  props: {
    activityItem: {
      type: Object,
      required: true,
    },
    prizeList: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      list: [],
      end_time: "",
      introduce: [],
      draw: 0,
      join_num: null,
      prizesold: [],
      prizesnew: [],
      // 天时分秒
      dd: "00",
      hh: "00",
      mm: "00",
      ss: "00",
      activity_id: "",
      openid: "",
      order_id: null,
      indexs: null,
      showAdress: false,
      winningData: {},
      btn_color: "",
    };
  },
  created() {
    console.log("activityItem", this.activityItem);
    this.list = this.prizeList.data;
    this.end_time = this.prizeList.end_time;
    this.introduce = this.prizeList.introduce;
    this.draw = this.prizeList.draw;
    this.join_num = this.prizeList.join_num;
    this.prizesold = this.activityItem.prizes;
    this.activity_id = this.activityItem.id;
    this.openid = localStorage.getItem("COPENID") || "obD_t1D_-_MgWCD3T3Jtk6GPdCUw";
    console.log("--------prizes--------", this.prizes);
    this.getPrizesList();
  },
  mounted() {
    console.log(this.prizesnew, this.activityItem, "111111");
    let time = setInterval(() => {
      if (this.flag == true) {
        clearInterval(time);
      }
      this.timeDown();
    }, 500);
  },
  methods: {
    // 关闭按钮
    btnClick() {
      this.click = true;
      this.showAdress = false;
    },
    // 跳到填写地址
    adressClick() {
      this.click = true;
      this.showAdress = false;
      this.$store.commit("setprizeItem", this.winningData.yes);
      this.$store.commit("setThemeColor", this.activityItem.theme_color);
      this.$router.push(
        `/caseShow/envelopesTalk/AdressForm?order_id=${this.order_id}&activity_id=${this.activityItem.id}&oid=3`
      );
    },
    // 自提奖品
    sinceClick() {
      this.click = true;
      this.showAdress = false;
    },
    // 继续抽奖
    continueClick() {
      this.click = true;
      this.showAdress = false;
      this.startCallBack();
    },
    getPrizesList() {
      this.prizesnew = [];
      console.log(this.prizesold, "this.prizesold;this.prizesold;");
      let data = this.prizesold;
      console.log(data, "data");
      if (!data) return;
      data.forEach((item, index) => {
        this.prizesnew.push({
          text: item.name,
          background: index % 2 ? "#f9e3bb" : "#f8d384",
          fonts: [{ text: item.name, top: "8%" }],
          imgs: [
            { src: item.picture, width: "30%", top: "35%", radius: "990px" },
          ],
        });
      });
    },
    startCallBack() {
      this.timer = true;
      if (this.timer) {
        this.timer = false;
        let params = { activity_id: this.activity_id, openid: this.openid };
        this.$request.doDraw(params).then((res) => {
          if (res.ret == 400) return this.$toast("活动还未开始,请开始后参与!");
          if (this.draw === 0)
            return this.$toast(
              "抽奖次数不足，" + "\n" + "请下单或邀请好友增加抽奖参数"
            );
          this.winningData = res.data;
          this.prize_type = res.data.prize_type;
          this.order_id = res.data.order_id;
          console.log(this.prize_type, "this.prize_typethis.prize_type");
          console.log(res.data, "res.data");
          console.log(res.data.sort, "res.data.sort");
          if (res.data == 1) return this.$toast("活动时间已结束");
          if (res.data == 2)
            return this.$toast(
              "无法继续抽奖," + "\n" + "您的总抽奖次数已达上限"
            );
          if (res.data == 3) return this.$toast("抽奖次数不足");
          this.$refs.LuckyWheel.play();
          this.indexs = res.data.sort - 1;
          setTimeout(() => {
            this.$refs.LuckyWheel.stop(res.data.sort - 1);
            // this.endCallBack(prize)
          }, 3000);
        });
      }
    },

    endCallBack(prize) {
      console.log(prize);
      this.showAdress = true;
      this.draw = this.draw - 1
      this.$emit("getPagePriz")

      // console.log(this.activityItem, this.prizeList, 'prizeListprizeListprizeListprizeList1234');w/envelopesTalk/AdressForm?order_id=${this.order_id}&activity_id=${this.activityItem.id}&oid=3`)}, 500)
      // setTimeout(() => {this.$router.push(`/caseSho
    },

    // 倒计时
    timeDown() {
      const endTime = new Date(this.end_time).getTime();
      const nowTime = new Date(this.end_time);
      var date = new Date();
      var now = date.getTime(nowTime);
      //设置截止时间
      var endDate = new Date(endTime);
      var end = endDate.getTime();
      //时间差
      var rightTime = end - now; //截止时间减去当前时间
      if (rightTime > 0) {
        //判断剩余倒计时时间如果大于0就执行倒计时否则就结束
        var dd = Math.floor(rightTime / 1000 / 60 / 60 / 24);
        var hh = Math.floor((rightTime / 1000 / 60 / 60) % 24);
        var mm = Math.floor((rightTime / 1000 / 60) % 60);
        var ss = Math.floor((rightTime / 1000) % 60);
        this.dd = dd > 9 ? dd : "0" + dd;
        this.hh = hh > 9 ? hh : "0" + hh;
        this.mm = mm > 9 ? mm : "0" + mm;
        this.ss = ss > 9 ? ss : "0" + ss;
      } else {
        this.isfail = true;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.tocaselotto {
  box-sizing: border-box;
  padding: 2rem 1rem 2rem;
  width: 100%;
  height: 100%;
  .top {
    // background-image: url(https://vkceyugu.cdn.bspapp.com/VKCEYUGU-d6d772e8-edba-4e51-a6a3-850f3326ab0d/4872975a-9026-4edb-b406-3194deea63dd.png);
    // background-color: #fff;

    box-sizing: border-box;
    .up {
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 100%;
      // height: 30px;
      // background-color: #fff;
      .img {
        box-sizing: border-box;
        display: flex;
        align-items: center;
        padding-top: 26px;
        width: 80%;
        height: 110px;
        img {
          width: 100%;
        }
      }
      .up-img {
        position: absolute;
        top: 1rem;
        right: 0;
        width: 7rem;
        height: 2.1rem;
        background-color: #ff0000;
        border-top-left-radius: 8px;
        border-bottom-left-radius: 8px;
        line-height: 2.1rem;
        font-size: 13px;
        text-align: center;
        color: #fff;
      }
      .title {
        word-wrap: break-word;
        word-break: normal;
        width: 70%;
        padding: 0.2rem 0;
        background-color: var(--btn_color);
        border-radius: 10px;
        line-height: 1.7rem;
        text-align: center;
        color: #fff;
        font-size: 1.4rem;
        margin-top: 0.3rem;
        letter-spacing: 0.3rem;
        border: 0;
      }
      .text {
        display: flex;
        align-items: center;
        color: #fff;
        .shu {
          margin: 0 8px;
        }
        .times {
        margin: 0.6rem 0;
        // color: #fddf43;
        :first-child {
          // margin-right: 12px;
        }
      }
      }
      
      .number_of {
        margin-bottom: 0.3rem;
        color: #fddf43;
      }
    }
  }

  .lottery-alert {
    max-width: 40rem;
    text-align: center;
    z-index: 10000;
    border-radius: 1rem;
    background: #fff;
    padding: 2rem;
    position: fixed;
    left: 0;
    right: 0;
    margin: auto 2rem;
    top: 50%;
    transform: translateY(-50%);
    .close-icon {
      position: fixed;
      width: 2.4rem;
      height: 2.4rem;
      right: 0.7rem;
      top: 0.7rem;
      border-radius: 1.2rem;
      display: flex;
      align-items: center;
      justify-content: center;
      z-index: 3002;
      background: var(--btn_color);
    }
  }
  .mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    position: fixed;
    overflow: hidden;
    z-index: 222;
    top: 0;
    left: 0;
  }

  .lottery-alert h1 {
    font-size: 1.8rem;
    font-weight: bold;
    color: #d92b2f;
  }

  .lottery-alert img {
    display: block;
    margin: 0 auto;
  }

  .lottery-alert h2 {
    font-weight: normal;
    color: #d92b2f;
    font-size: 1.5rem;
    padding-top: 1.5rem;
  }

  .lottery-alert p {
    color: #666;
    font-size: 1.6rem;
    padding-top: 0.5rem;
  }

  .lottery-alert .btnsave {
    border-radius: 0.3rem;
    box-shadow: none;
    height: 4rem;
    cursor: pointer;
    line-height: 4rem;
    color: #fff;
    margin-top: 1.2rem;
    background: var(--btn_color);
    font-size: 1.4rem;
  }
}
</style>